package com.example.materialdesigndemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main.toolbar
import kotlinx.android.synthetic.main.activity_news.*
import java.lang.StringBuilder

class NewsActivity : AppCompatActivity() {
    companion object {
        val NEWS_TITLE = "news_title"
        val NEWS_CONTENT = "news_content"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_news)

        val newsTitle = intent.getStringExtra(NEWS_TITLE)
        val newsContent = intent.getStringExtra(NEWS_CONTENT)


        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)

        newsTitle?.let {
            collapsingToolbar.title = newsTitle
        }
        newsContent?.let {
            newsContentTv.text = generateNewsContent500Times(newsContent)
        }

    }

    fun generateNewsContent500Times(newsContent: String): String {
        val stringBuilder = StringBuilder()
        for (times in 1..500) {
            stringBuilder.append(newsContent)
        }
        return stringBuilder.toString()
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> {
                finish()
                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }
}
